package mest.controller.Modeling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import mest.VO.hank.model.customer_fileModel;
import mest.VO.hank.model.provider_fileModel;
import mest.entity.customer_file;
import mest.entity.provider_file;
import mest.entity.provider_type;
import mest.service.Modeling.IprovideFileService;
import mest.util.ResultObj;
import mest.util.mestUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-15 23:27
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/provideFile")
public class provideFile {
    @Resource
    private IprovideFileService ipfs;


    @RequestMapping(value = "/insert")
    public String insert(@RequestBody JSONObject json, HttpServletRequest request) {
        provider_file record = json.toJavaObject(provider_file.class);
        String theirDb = mestUtil.getCompanyDB(request);
        ResultObj obj = ipfs.insertProvideFile(record, theirDb);
        return JSON.toJSONString(obj);

    }


    @RequestMapping(value = "/baseSelect", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String baseSelect(int index, int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        List<provider_file> provideFilePage = ipfs.findProvideFilePage(index, pageSize, theirDb);
        int i = ipfs.selectCount(theirDb);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", i);
        map.put("data", provideFilePage);
        return JSON.toJSONString(map);
    }


    /**
     * 删除方法
     */
    @RequestMapping("/delModel")
    public Object delModel(@RequestBody JSONObject json, HttpServletRequest request) {
        Map<String, Object> map = json.toJavaObject(Map.class);
        Integer id = (Integer) map.get("id");
        System.out.println("这里的ID是：" + id);
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if (ipfs.delModel(id, theirDb) > 0) {
            modelMap.put("erg", "删除成功");
            return JSON.toJSONString(modelMap);
        } else {
            modelMap.put("erg", "不成功");
            return JSON.toJSONString(modelMap);
        }
    }

    @RequestMapping("/queryAll")
    public Object queryAll(Integer index, Integer pageSize, String htmlModel, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String, Object> modelMap = new HashMap<String, Object>();

        try {
            List<provider_fileModel> eModel = ipfs.queryAll(index, pageSize, htmlModel, theirDb);
            if (!ObjectUtils.isEmpty(eModel)) {
                modelMap.put("data", eModel);
                modelMap.put("msg", "success");
                modelMap.put("code", 0);
                modelMap.put("count", ipfs.findCount(htmlModel, theirDb));
            } else {
                modelMap.put("msg", "此对象为空值");
            }

        } catch (Exception e) {
            modelMap.put("msg", "fales");
            modelMap.put("errmsg", e.toString());
        }
        return JSON.toJSONString(modelMap);
    }


    /**
     * 动态更新某一个值或多个值
     *
     * @param json
     * @return
     */
    @RequestMapping("/updateByOne")
    public ResultObj updateByOne(@RequestBody JSONObject json, HttpServletRequest request) {
        try {
            String theirDb = mestUtil.getCompanyDB(request);
            System.out.println("==============================================");
            System.out.println("provider_file is " + json.toString() + "; provider_file is " + json);
            Map<String, Object> modelMap = new HashMap<String, Object>();
            provider_file record = json.toJavaObject(provider_file.class);
            if (ipfs.updateByOne(record, theirDb) > 0) {
                modelMap.put("erg", "删除成功");
                return ResultObj.createSuccessMsg(ResultObj.UPDATE_SUCCESS);
            } else {
                modelMap.put("erg", "不成功");
                return ResultObj.createErrorMsg(ResultObj.ERROR2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ResultObj.createErrorMsg(e.getMessage());
            return ResultObj.createErrorMsg(ResultObj.ERROR2);
        }
    }


}
